﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using NUnit.Framework;

namespace QandAMongoDbConsole
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }

    public class QuestionRepository
    {
        private IMongoCollection _questions;

        public QuestionRepository()
        {
            var mongo = new Mongo();
            mongo.Connect();
            var db = mongo.GetDatabase("questionAndAnswer");
            _questions = db.GetCollection("question");
            
        }

        public Document FindOne(Document doc)
        {
            return _questions.FindOne(doc);
        }
        
        public void Update(Document doc)
        {
            _questions.Update(doc);
        }


        public void Insert(Document doc)
        {
            _questions.Insert(doc);
        }
    }

    [TestFixture]
    public class MongoTests
    {
        [Test]
        public void When_Then()
        {
            var connected = new QuestionRepository();
            
            var doc = connected.FindOne(new Document() {{"title", "Star wars"}});

            Assert.That(doc["title"], Is.EqualTo("Star Wars"));
        }

        [Test]
        public void Should_Update_Document()
        {
            var connected = new QuestionRepository();
            
            var spec = new Document() {{"title", "Star Wars"}};
            var doc = connected.FindOne(spec);

            doc["title"] = "Not star wars";

            connected.Update(doc);

            doc = connected.FindOne(new Document(){{"title", "Not star wars"}});
            Assert.That(doc["title"], Is.EqualTo("Not star wars"));
        }

        [Test]
        public void Should_be_able_to_add_question()
        {
            var connected = new QuestionRepository();
            var doc = new Document();
            doc.Add("Title", "Best phone");
            doc.Add("Description", "Waht is the best phone here");
            doc.Add("Answers", "[{\"Text\": \"Android\",\"IsCorrect\": true}, {\"Text\": \"iSnore\" }]");
//            connected.Insert(new
//                                 {
//                                     Question = "Is the iPhone the coolest phone in the room?",
//                                     QuestionTitle = "Is the iPhone the coolest phone in the room?",
//                                     
////                                     Answer = new[] {new {Text = "Yes", IsCorrect=true}, new {Text = "No", IsCorrect=false}}
//                                 });
            var questionSpec = new Document() { { "Title", "what is the coolest phone in this room?" }, 
                {"answers", "[]"}};
            connected.Insert(doc);
            
            doc = connected.FindOne(questionSpec);
            Assert.That(doc["Title"], Is.EqualTo("Best phone"));
            Assert.That(doc["Title.Answers"]);
        }


    }

}
